package com.wsd.util.log;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;

import com.wsd.util.WebApplicationContextUtils;
public class WsdLogFactory {
	static{
		init();
	}
	public static Log getLog(String logName){
		return LogFactory.getLog(logName);
	}
	/**
	 * 程序中得到log
	 * @param clazz
	 * @return
	 */
	public static Log getLog(Class clazz){
		return LogFactory.getLog(clazz);
	}
	public static void init(){
		//Logger log = Logger.getLogger(Log4jConfig.class);
		//log.info("正在初始化Log4j");
		//得到配置文件
		Log4jConfig log4jConfig = (Log4jConfig) WebApplicationContextUtils.getBean("log4jConfig");
		
		String prefix = WebApplicationContextUtils.getServletContext().getRealPath("/");
		String file = log4jConfig.getLog4jPath();
		String filePath = prefix + file;
		Properties props = new Properties();
		

		FileInputStream log4jStream;
		try {
			log4jStream = new FileInputStream(filePath);
			props.load(log4jStream);
	        log4jStream.close();
	        for(String logName:log4jConfig.getLogMap().keySet()){
	        	String logFile = prefix + log4jConfig.getLogMap().get(logName); //设置路径
	        	props.setProperty("log4j.appender." + logName + ".File", logFile);
	        }
	        PropertyConfigurator.configure(props); //装入log4j配置信息
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//log.info("Log4j初始化完成");
	}

}
